/**
 * Copyright © 2013 Creativor Studio.
 * About license information, please see LICENSE.txt.
 */
package org.creativor.rayson.client.proxy;

import java.net.InetSocketAddress;

import org.creativor.rayson.api.Protocol;

/**
 * An key of one RPC proxy.
 * 
 * @author creativor
 */
final class ProxyKey {

	private final Class<? extends Protocol> protocol;
	private final ClassLoader loader;
	private final InetSocketAddress serverAddr;

	/**
	 * @param serverAddr
	 * @param classLoader
	 * @param protocol
	 */
	ProxyKey(final ClassLoader loader, final InetSocketAddress serverAddr,
			final Class<? extends Protocol> protocol) {
		this.loader = loader;
		this.serverAddr = serverAddr;
		this.protocol = protocol;
	}

	@Override
	public boolean equals(final Object obj) {
		if (obj == null)
			return false;
		if (!(obj instanceof ProxyKey))
			return false;
		final ProxyKey that = (ProxyKey) obj;
		if (this.loader != that.loader)
			return false;
		if (!this.protocol.getName().equals(that.protocol.getName()))
			return false;
		if (!this.serverAddr.equals(that.serverAddr))
			return false;
		return true;
	}

	/**
	 * @return the loader
	 */
	public ClassLoader getLoader() {
		return loader;
	}

	/**
	 * @return the protocol
	 */
	public Class<? extends Protocol> getProtocol() {
		return protocol;
	}

	/**
	 * @return the serverAddr
	 */
	public InetSocketAddress getServerAddr() {
		return serverAddr;
	}

	@Override
	public int hashCode() {
		return this.loader.hashCode() + this.serverAddr.hashCode()
				+ this.protocol.hashCode();
	}

	@Override
	public String toString() {
		final StringBuffer sb = new StringBuffer();
		sb.append("{");
		sb.append("loader:" + this.loader);
		sb.append(", protocol:" + this.protocol);
		sb.append("}");
		return sb.toString();
	}
}
